package com.atguigu.gamll_sugar.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gamll_sugar.entity.*;
import com.atguigu.gamll_sugar.service.KeywordStatsService;
import com.atguigu.gamll_sugar.service.ProductStatsService;
import com.atguigu.gamll_sugar.service.ProvinceStatsService;
import com.atguigu.gamll_sugar.service.VisitorStatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

import static com.atguigu.gamll_sugar.util.CommonUtils.*;

/**
 * @author chenlongStart
 * @create 2021-07-07 13:19
 * @desc
 */
@RestController
public class SugarController {

    @Autowired
    ProductStatsService product;
    @Autowired
    ProvinceStatsService province;
    @Autowired
    VisitorStatsService visitor;
    @Autowired
    KeywordStatsService keyword;

    @RequestMapping("/sugar/gmv")
    public String method1(@RequestParam(value = "date", defaultValue = "0") int date) {

        date = defaultDate(date);

        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        result.put("data", product.getGMV(date));

        return result.toJSONString();
    }

    @RequestMapping("/sugar/tm_gmv")
    public String method2(@RequestParam(value = "date", defaultValue = "0") int date,
                          @RequestParam(value = "limit", defaultValue = "5") int limit) {
        date = defaultDate(date);

        List<TmStats> list = product.getTmGMV(date, limit);
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        JSONObject data = new JSONObject();
        JSONArray categories = new JSONArray();
        JSONArray series = new JSONArray();
        JSONObject obj = new JSONObject();
        JSONArray tmData = new JSONArray();
        for (TmStats productStats : list) {
            categories.add(productStats.getTm_name());
            tmData.add(productStats.getOrder_amount());
        }
        obj.put("name", "商品品牌");
        obj.put("data", tmData);
        series.add(obj);
        data.put("categories", categories);
        data.put("series", series);
        result.put("data", data);

        return result.toJSONString();
    }


    @RequestMapping("/sugar/c3_gmv")
    public String method3(@RequestParam(value = "date", defaultValue = "0") int date,
                          @RequestParam(value = "limit", defaultValue = "5") int limit) {
        date = defaultDate(date);
        List<C3Stats> list = product.getC3GMV(date, limit);
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        JSONArray data = new JSONArray();
        for (C3Stats c3Stats : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", c3Stats.getCategory3_name());
            obj.put("value", c3Stats.getOrder_amount());
            data.add(obj);
        }
        result.put("data", data);

        return result.toJSONString();
    }


    @RequestMapping("/sugar/spu_gmv")
    public String method4(@RequestParam(value = "date", defaultValue = "0") int date,
                          @RequestParam(value = "limit", defaultValue = "10") int limit) {
        date = defaultDate(date);
        List<SpuStats> list = product.getSpuGMV(date, limit);
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        JSONObject data = new JSONObject();
        JSONArray columns = new JSONArray();
        JSONObject c1 = new JSONObject();
        c1.put("name", "spu名字");
        c1.put("id", "spu_name");
        columns.add(c1);
        JSONObject c2 = new JSONObject();
        c2.put("name", "销售额");
        c2.put("id", "order_amount");
        columns.add(c2);
        data.put("columns", columns);
        JSONArray rows = new JSONArray();
        for (SpuStats spuStats : list) {
            JSONObject obj = new JSONObject();
            obj.put("spu_name", spuStats.getSpu_name());
            obj.put("order_amount", spuStats.getOrder_amount());
            rows.add(obj);
        }
        data.put("rows", rows);
        result.put("data", data);

        return result.toJSONString();
    }


    @RequestMapping("/sugar/province_gmv")
    public String method5(@RequestParam(value = "date", defaultValue = "0") int date) {
        date = defaultDate(date);
        List<ProvinceStats> list = province.getProvinceGMV(date);
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        JSONObject data = new JSONObject();
        JSONArray mapData = new JSONArray();
        for (ProvinceStats provinceStats : list) {
            JSONObject obj = new JSONObject();
            JSONArray tooltipValues = new JSONArray();
            obj.put("name", provinceStats.getProvince_name());
            obj.put("value", getRandomNum());
            tooltipValues.add(provinceStats.getOrder_count());
            tooltipValues.add(provinceStats.getOrder_amount());
            obj.put("tooltipValues", tooltipValues);
            mapData.add(obj);
        }
        data.put("mapData", mapData);
        data.put("valueName", "人口");
        JSONArray tooltipNames = new JSONArray();
        tooltipNames.add("订单数");
        tooltipNames.add("销售额");
        data.put("tooltipNames", tooltipNames);
        JSONArray tooltipUnits = new JSONArray();
        tooltipUnits.add("个");
        tooltipUnits.add("元");
        data.put("tooltipUnits", tooltipUnits);

        result.put("data", data);
        return result.toJSONString();
    }


    @RequestMapping("/sugar/visitor_gmv")
    public String method6(@RequestParam(value = "date", defaultValue = "0") int date) {
        date = defaultDate(date);
        Map<String, VisitorStats> map = visitor.getVisitorGMV(date);
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        JSONObject data = new JSONObject();
        JSONArray categories = new JSONArray();
        for (int i = 0; i < 24; i++) {
            categories.add(formatDecimal(i));
        }
        data.put("categories", categories);
        JSONArray series = new JSONArray();
        JSONObject pv = new JSONObject();
        pv.put("name", "pv");
        JSONArray innerData = new JSONArray();
        for (int i = 0; i < 24; i++) {
            VisitorStats vs = map.get(i + "");
            if (vs == null) {
                innerData.add(0);
            } else {
                innerData.add(vs.getPv());
            }
        }
        pv.put("data", innerData);
        series.add(pv);

        JSONObject uv = new JSONObject();
        uv.put("name", "uv");
        JSONArray innerData1 = new JSONArray();
        for (int i = 0; i < 24; i++) {
            VisitorStats vs = map.get(i + "");
            if (vs == null) {
                innerData1.add(0);
            } else {
                innerData1.add(vs.getUv());
            }
        }
        uv.put("data", innerData1);
        series.add(uv);

        JSONObject uj = new JSONObject();
        uj.put("name", "uj");
        JSONArray innerData2 = new JSONArray();
        for (int i = 0; i < 24; i++) {
            VisitorStats vs = map.get(i + "");
            if (vs == null) {
                innerData2.add(0);
            } else {
                innerData2.add(vs.getUj());
            }
        }
        uj.put("data", innerData2);
        series.add(uj);

        JSONObject sv = new JSONObject();
        sv.put("name", "sv");
        JSONArray innerData3 = new JSONArray();
        for (int i = 0; i < 24; i++) {
            VisitorStats vs = map.get(i + "");
            if (vs == null) {
                innerData3.add(0);
            } else {
                innerData3.add(vs.getSv());
            }
        }
        sv.put("data", innerData3);
        series.add(sv);


        data.put("series", series);

        result.put("data", data);
        return result.toJSONString();
    }


    @RequestMapping("/sugar/isNew_gmv")
    public String method7(@RequestParam(value = "date", defaultValue = "0") int date) {
        date = defaultDate(date);

        List<VisitorStats> list = visitor.statsByIsNew(date);

        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");

        JSONObject data = new JSONObject();
        data.put("total", list.size());
        JSONArray columns = new JSONArray();
        JSONObject c1 = new JSONObject();
        c1.put("name", "新老用户");
        c1.put("id", "is_new");
        columns.add(c1);

        JSONObject c2 = new JSONObject();
        c2.put("name", "uv");
        c2.put("id", "uv");
        columns.add(c2);

        JSONObject c3 = new JSONObject();
        c3.put("name", "pv");
        c3.put("id", "pv");
        columns.add(c3);

        JSONObject c4 = new JSONObject();
        c4.put("name", "uj");
        c4.put("id", "uj");
        columns.add(c4);

        JSONObject c5 = new JSONObject();
        c5.put("name", "sv");
        c5.put("id", "sv");
        columns.add(c5);
        data.put("columns", columns);

        JSONArray rows = new JSONArray();

        for (VisitorStats vs : list) {
            JSONObject obj = new JSONObject();
            obj.put("is_new", vs.getIs_new());
            obj.put("pv", vs.getPv());
            obj.put("uv", vs.getUv());
            obj.put("uj", vs.getUj());
            obj.put("sv", vs.getSv());
            rows.add(obj);
        }
        data.put("rows", rows);
        result.put("data", data);
        return result.toJSONString();
    }

    @RequestMapping("/sugar/kw_gmv")
    public String method8(@RequestParam(value = "date", defaultValue = "0") int date,
                          @RequestParam(value = "limit", defaultValue = "20") int limit) {
        date = defaultDate(date);

        List<KeywordStats> list = keyword.selectKeywordStats(date, limit);

        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");

        JSONArray data = new JSONArray();
        for (KeywordStats kw : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", kw.getKeyword());
            obj.put("value", kw.getCt());
            data.add(obj);
        }


        result.put("data", data);

        return result.toJSONString();

    }


}
